'''
Created on Aug 22, 2011

@author: nktran
'''
import re
import urllib
import urllib2

from constant import URL, PREPOSITION_LIST

def fetch(query=""):
    """
    """
    values ={'q': query,
             'SearchBtn':'',
             'database':0
             }
    data = urllib.urlencode(values)
    request = urllib2.Request(URL, data)
    response = urllib2.urlopen(request)
    content = response.read()
    
    return content

def make_pattern(string, subj='X', obj='Y'):
    """
    """
    string = re.sub("<b>|</b>"," ",string)
    phrase = "X"
    for id, substr in enumerate(re.split("&lt;|&gt;", string)):
        substr = substr.strip()
        #print substr, substr[2:len(substr)-2]
        if id == 0 and 'obj' in substr[2:len(substr)-2]:
            phrase = phrase + " " + "BE"   
        if ':' in substr and substr[2:len(substr) - 2] == 'nn':
            phrase = phrase + " " + 'on'     
        if ':' in substr and substr[2:len(substr)-2] in PREPOSITION_LIST:
            phrase = phrase + " " + substr[2:len(substr) - 2]
        if ":" not in substr:
            phrase = phrase + " " + substr
            
    phrase = phrase + " " + 'Y'
    return phrase
            
def parse(content="", subj='X', obj='Y'):
    ""
    ""
    lines = content.split("\n")
    for line_id,line in enumerate(lines):
        if '<div class="post">' in line:
            target_line = lines[line_id+1]
            break
        
    pos = target_line.find('<table>') 
    phrase = make_pattern(target_line[:pos])
    para_list = [phrase]
    for id, line in enumerate(target_line.split('</tr>')):
        start_p = line.find('<tr><td>')
        end_p = line.find('</td>')
        if start_p == -1 or end_p == -1:
            break
        start_p = line.find('<td>', end_p)
        end_p = line.find('</td', start_p)
        
        paraphrase = make_pattern(line[start_p + 4: end_p])
        para_list.append(paraphrase)
        
    return para_list

def output(query_id, para_list=None):
    """
    """
    fout = open('rules.' + str(query_id), 'w')
    try:
        fout.write('#' + para_list[0] + '\n')
        for rule in para_list[1:]:
            fout.write(rule + "\n")
    finally:
        fout.close()
        
if __name__ == '__main__':   
    query_list = ['X work for Y', 'X is attacked by Y']
    for id, query in enumerate(query_list):      
        para_list = parse(fetch(query))
        output(id, para_list)
    